package net.sudot.chess.plugin.storage.local;

import net.sudot.chess.config.SystemProperties;
import net.sudot.chess.exception.BusinessException;
import net.sudot.chess.plugin.BasePlugin;
import net.sudot.chess.plugin.PluginAttributeConfig;
import net.sudot.chess.plugin.storage.StoragePlugin;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 本地文件存储插件
 *
 * @author tangjialin on 2018-03-18.
 */
@Component
public class LocalStoragePlugin extends StoragePlugin {

    @Resource
    private SystemProperties systemProperties;

    @Override
    public String getName() {
        return "本地文件存储";
    }

    @Override
    public List<PluginAttributeConfig> getAttributeConfigs() {
        return null;
    }

    @Override
    public boolean supports(HttpServletRequest request) {
        return false;
    }

    @Override
    public void upload(String path, File file, String contentType) {
        File destFile = new File(systemProperties.getUploadPath() + path);
        try {
            FileUtils.moveFile(file, destFile);
        } catch (IOException e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    @Override
    public String getDownloadUrl(String path) {
        return "";
    }

    @Override
    public int compareTo(BasePlugin plugin) {
        return 0;
    }
}